package com.ruoyi.network.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.JSONWriter.Feature;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.event.NetworkEvent;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.common.websocket.handler.AbstractWebsocketMsgHandler;
import com.ruoyi.network.dao.NetworkEdgeDao;
import com.ruoyi.network.domain.NetworkEdge;
import com.ruoyi.network.service.INetworkEdgeService;

/**
 * 边缘端Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-09-18
 */
@Service
public class NetworkEdgeServiceImpl implements INetworkEdgeService 
{
    @Autowired
    private NetworkEdgeDao networkEdgeDao;
    @Autowired
	private ApplicationEventPublisher eventPublisher;

    /**
     * 查询边缘端
     * 
     * @param id 边缘端主键
     * @return 边缘端
     */
    @Override
    public NetworkEdge selectById(String id)
    {
        return networkEdgeDao.selectById(id);
    }

    /**
     * 查询边缘端列表
     * 
     * @param networkEdge 边缘端
     * @return 边缘端
     * @throws Exception
     */
    @Override
    public List<Map<String, Object>> queryList(Map<String, Object> param) throws Exception
    {
        List<Map<String, Object>> list = networkEdgeDao.queryList(param);
        //BeanUtils.caseFormat(list); 在Mybatis拦截器插件中统一处理
		return list;
    }
    
    /**
     * 查询边缘端列表
     * 
     * @param networkEdge 边缘端
     * @return 边缘端
     */
    @Override
    public List<NetworkEdge> selectList(NetworkEdge networkEdge)
    {
        return networkEdgeDao.selectList(networkEdge);
    }

    /**
     * 新增边缘端
     * 
     * @param networkEdge 边缘端
     * @return 结果
     */
    @Override
    public int insert(NetworkEdge networkEdge)
    {
        networkEdge.setCreateTime(DateUtils.getNowDate());
        return networkEdgeDao.insert(networkEdge);
    }

    /**
     * 修改边缘端
     * 
     * @param networkEdge 边缘端
     * @return 结果
     */
    @Override
    public int update(NetworkEdge networkEdge)
    {
        networkEdge.setUpdateTime(DateUtils.getNowDate());
        return networkEdgeDao.update(networkEdge);
    }

    /**
     * 批量删除边缘端
     * 
     * @param ids 需要删除的边缘端主键
     * @return 结果
     */
    @Override
    public int deleteByIds(String[] ids)
    {
        return networkEdgeDao.deleteByIds(ids);
    }

    /**
     * 删除边缘端信息
     * 
     * @param id 边缘端主键
     * @return 结果
     */
    @Override
    public int deleteById(String id)
    {
        return networkEdgeDao.deleteById(id);
    }
    @Override
    public int diableOtherEdges(String id) {
    	return networkEdgeDao.diableOtherEdges(id);
    	
    }
    @Override
    public AjaxResult change(NetworkEdge networkEdge) {
    	NetworkEdge bean = selectById(networkEdge.getId());
    	boolean isOnline = bean.getOnline() != null && bean.getOnline().intValue() == 1;
		if(isOnline) {
    		int update = update(networkEdge);
    		boolean isActive = networkEdge.getStatus().equals("0");
    		if(isActive) {
    			diableOtherEdges(networkEdge.getId());
    		}
    		
        	//boolean isConfigProxy = update > 0 && StringUtils.isNotBlank(networkEdge.getProxyIp()) && StringUtils.isNotBlank(networkEdge.getProxyPort());
        	boolean isConfigProxy = update > 0;
			if(isConfigProxy) {
        		JSONObject res = AbstractWebsocketMsgHandler.getCommandTemplate("90001",new HashMap<String, Object>(){{
        			put("protocol", 10000);
        			put("seqNum", AbstractWebsocketMsgHandler.getRandomNum(9));
        			put("terminateId", networkEdge.getId());
        			put("isActive", isActive);
        			put("innerHost", networkEdge.getProxyIp());
        			put("innerPort", networkEdge.getProxyPort());
        		}});
        		//WebSocketUtils.sendGroupMessage("edge", res);//不直接发送
        		//统一交给ruoyi-network-server模块处理,方便代码维护
        		String text = JSON.toJSONString(res,Feature.WriteMapNullValue);
    			eventPublisher.publishEvent(new NetworkEvent(this, text));
        	}
    		return update > 0 ? AjaxResult.success() : AjaxResult.error();
    	}
    	return AjaxResult.error("边缘端不在线");
    	
    }
}
